package zust.se.delicious.action;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import zust.se.delicious.domain.Tags;
import zust.se.delicious.service.BookmarkService;
import zust.se.delicious.vo.BookmarkBean;
import zust.se.delicious.vo.SessionUserBean;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SearchAction extends ActionSupport {
	private String keyword;
	private String searchUrl;

	public String getSearchUrl() {
		return searchUrl;
	}

	public void setSearchUrl(String searchUrl) {
		this.searchUrl = searchUrl;
	}

	private BookmarkService bookmarkService;
	private List<BookmarkBean> myList;
	private List<BookmarkBean> otherList;
	private Set<Tags> tagList;
	private int result;

	public String execute() {
		if (keyword != null) {
			Map session = ActionContext.getContext().getSession();
			SessionUserBean sessionUser = (SessionUserBean) session.get("sessionUser");
			if (sessionUser != null) {
				int uid = sessionUser.getId();
				myList = bookmarkService.searchMyList(keyword.trim(), uid);
				otherList = bookmarkService.searchOtherList(keyword.trim(), uid);
				result = myList.size() + otherList.size();
			} else {
				otherList = bookmarkService.searchAllList(keyword.trim());
				result = otherList.size();
			}
			tagList = new HashSet(bookmarkService.searchTagList(keyword.trim()));

		}
		return SUCCESS;
	}
	
	public String mathchUrl() {
		Map session = ActionContext.getContext().getSession();
		SessionUserBean sessionUser = (SessionUserBean) session.get("sessionUser");
		if (sessionUser != null) {
			int uid = sessionUser.getId();
			myList = bookmarkService.matchMyList(searchUrl, uid);
			otherList = bookmarkService.matchOtherList(searchUrl, uid);
			result = myList.size() + otherList.size();
		} else {
			otherList = bookmarkService.matchAllList(searchUrl);
			result = otherList.size();
		}
		tagList = new HashSet(bookmarkService.matchTagList(searchUrl));
		return SUCCESS;
	}

	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public void setOtherList(List<BookmarkBean> otherList) {
		this.otherList = otherList;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public List<BookmarkBean> getMyList() {
		return myList;
	}

	public List<BookmarkBean> getOtherList() {
		return otherList;
	}

	public Set<Tags> getTagList() {
		return tagList;
	}

	public void setBookmarkService(BookmarkService bookmarkService) {
		this.bookmarkService = bookmarkService;
	}

}
